//
//  MeViewController.swift
//  scfriend
//
//  Created by wulanzhou on 2017/8/30.
//  Copyright © 2017年 wulanzhou. All rights reserved.
//

import UIKit

class MeViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    
    private var Me_HeaderView_Height:CGFloat = 213.0
    
    private lazy var meTable:UITableView = {
        
        //let tab = UITableView.init(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height), style: .plain)
        let tab = UITableView.init(frame: CGRect(x: 0, y: kNavigationBarHeight, width: self.view.bounds.size.width, height: self.view.bounds.size.height - kNavigationBarHeight), style: .plain)
        tab.dataSource = self
        tab.delegate = self
        tab.backgroundColor = UIColor.clear
        tab.separatorStyle = .none
        tab.contentInset = UIEdgeInsets(top: self.Me_HeaderView_Height - kNavigationBarHeight, left: 0, bottom: 0, right: 0)
        /**
        let footer:UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height - 5 * 44.0 - 4 * 10.0))
        footer.backgroundColor = UIColor.withHex(hexString: "efeff4")
        tab.tableFooterView = footer
        **/
        
        let headerView:SegmentMenuView = SegmentMenuView.init(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 60.0))
        
        tab.tableHeaderView = headerView
        return tab
        
    }()
    
    private lazy var svHeader:MeHeaderView = {
        
        weak var WSELF = self
        let sv = MeHeaderView.init(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.Me_HeaderView_Height))
        sv.setBackgroundImage(image: UIImage.init(named: "person_defaultback_image")!)
        sv.setHeader(image: UIImage.init(named: "default_head")!,text: "kingboyrang")
        return sv
        
    }()
    
    private var listData:[[Dictionary<String,String>]] = [[["image":"latest_events","name":"最新活动"]],
                                                          [["image":"use_raiders","name":"使用攻略"]],
                                                          [["image":"about_us","name":"关于我们"],["image":"online_service","name":"在线客服"]],
                                                          [["image":"setting","name":"设置"]]]
    

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        

        self.automaticallyAdjustsScrollViewInsets = false
        self.view.addSubview(self.meTable)
        //插入视图在某个视图之下
        self.view.insertSubview(self.svHeader, belowSubview: self.meTable)
        
        // navigationBar 的颜色可以根据这个方法来调整
        weak var WSELF = self
        self.svHeader.scrollViewStateChangeWithBlock { (reachtop:Bool) in
            WSELF?.changeNavigationBarColor(isClear: !reachtop)
        }
        
        
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //默认不显示导航
        self.changeNavigationBarColor(isClear: true)
        //调整显示
        self.svHeader.reloadWithScrollView(scrollView: self.meTable)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        //还原导航
        self.changeNavigationBarColor(isClear: false)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    private func changeNavigationBarColor(isClear:Bool){
        if(isClear){
           self.navigationController?.navigationBar.ch_setBackgroundColor(UIColor.clear)
        }else{
           self.navigationController?.navigationBar.ch_removeBackgroundColor()
        }
        
    }
    
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.svHeader.reloadWithScrollView(scrollView: scrollView)
        
        print("scrollView.contentOffset.y = \(scrollView.contentOffset.y)")
    }
    
    //UITableViewDataSource & UITableViewDelegate
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.listData.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.listData[section].count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell:LineTableViewCell? = tableView.dequeueReusableCell(withIdentifier: "reUse") as? LineTableViewCell
        
        if cell == nil {
            cell = LineTableViewCell.init(style: .default, reuseIdentifier: "reUse", showTopLine: (indexPath.row == 0 ? false:true))
            cell?.accessoryType = .disclosureIndicator
        }
        let item:Dictionary<String,String> = self.listData[indexPath.section][indexPath.row]
        cell?.textLabel?.font = UIFont.systemFont(ofSize: 14.0)
        cell?.textLabel?.text = item["name"]
        cell?.imageView?.image = UIImage.init(named: item["image"]!)
        return cell!
        
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10.0
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView:UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 10))
        headerView.backgroundColor = UIColor.withHex(hexString: "efeff4")
        return headerView
    }
    /***
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        print(" section = \(section)")
        if (section == self.listData.count - 1){
            
            return self.view.bounds.size.height - 5 * 44.0 - 4 * 10.0
        }
        return 0.0
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        if (section == self.listData.count - 1){
            var r = self.view.bounds
            r.origin.y = -44.0
            r.size.height -= 5 * 44.0 + 4 * 10.0
            let headerView:UIView = UIView.init(frame: r)
            headerView.backgroundColor = UIColor.withHex(hexString: "efeff4")
            return headerView
        
        }
        return nil
    }
   ***/
 
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}
